import fromUnixTime from 'date-fns/fromUnixTime'
// import differenceInDays from 'date-fns/differenceInDays'
import Cookies from 'js-cookie'
import {
  ANALYTICS_IDENTITY,
  ANALYTICS_RESET,
  CHATWOOT_RESET,
  CHATWOOT_SET_USER,
} from '../../helper/scriptHelpers'
import { LocalStorage, LOCAL_STORAGE_KEYS } from '../../helper/localStorage'

Cookies.defaults = { sameSite: 'Lax' }

export const getLoadingStatus = state => state.fetchAPIloadingStatus
export const setLoadingStatus = (state, status) => {
  state.fetchAPIloadingStatus = status
}

export const setUser = user => {
  window.bus.$emit(CHATWOOT_SET_USER, { user })
  window.bus.$emit(ANALYTICS_IDENTITY, { user })
}

export const getHeaderExpiry = response =>
  fromUnixTime(response.data.expiry)

export const setAuthCredentials = response => {
  const expiryDate = getHeaderExpiry(response)
  // const expires = differenceInDays(expiryDate, new Date())
  // Cookies.set('cw_d_session_info', response.data, {
  //   expires: expires,
  // })
  Cookies.set('cw_d_session_info', response.data)
  setUser(response.data.user, expiryDate)
}

export const clearBrowserSessionCookies = () => {
  Cookies.remove('cw_d_session_info')
  Cookies.remove('auth_data')
  Cookies.remove('user')
  Cookies.remove('__tenant')
}

export const clearLocalStorageOnLogout = () => {
  LocalStorage.remove(LOCAL_STORAGE_KEYS.DRAFT_MESSAGES)
}

export const clearCookiesOnLogout = () => {
  window.bus.$emit(CHATWOOT_RESET)
  window.bus.$emit(ANALYTICS_RESET)
  clearBrowserSessionCookies()
  clearLocalStorageOnLogout()
  const globalConfig = window.globalConfig || {}
  const logoutRedirectLink = globalConfig.LOGOUT_REDIRECT_LINK || '/'
  window.location = logoutRedirectLink
}

export const parseAPIErrorResponse = error => {
  if (error?.response?.data?.message) {
    return error?.response?.data?.message
  }
  if (error?.response?.data?.error) {
    return error?.response?.data?.error
  }
  return error
}

export const throwErrorMessage = error => {
  const errorMessage = parseAPIErrorResponse(error)
  throw new Error(errorMessage)
}
